package com.basic.aqs;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBrrierTest {

	private static final CyclicBarrier BARRIER = new CyclicBarrier(2);
	
	@SuppressWarnings("all")
	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(1000);
					try {
						System.out.println("t1 执行完成开始等待！");
						BARRIER.await();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					System.out.println("t1 执行了！");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(3000);
					System.out.println("t2 执行完成开始等待！");
					try {
						BARRIER.await();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
					System.out.println("t2 执行了！");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println("main 执行了！");
		BARRIER.reset();
		System.out.println(BARRIER.getParties());
		System.out.println(BARRIER.getNumberWaiting());
	}
}
